package org.atovio.coolexam.exception.handler;

import org.atovio.coolexam.common.HttpCode;
import org.atovio.coolexam.common.Result;
import org.atovio.coolexam.exception.AuthenticationException;
import org.atovio.coolexam.exception.ServiceException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;
import java.util.StringJoiner;

@RestControllerAdvice
public class ServerExceptionHandler {

    // 处理其他异常
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        e.printStackTrace();
        return Result.serverFail();
    }

    // 业务异常处理
    @ExceptionHandler(ServiceException.class)
    public Result handleServiceException(ServiceException e) {
        // 向前端返回错误信息和状态码
        return Result.fail(e.getCode(), e.getMessage());
    }
}
